﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Data.Common;
using TSV.Library.Common;
using TSV.Models;

namespace TSV.Areas.Administration.Models
{
    public class CollaboratorDao : BaseDao
    {
        #region Public methods

        /// <summary>
        /// Get By Id
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public Collaborator GetById(int ID)
        {
            return dbContext.Collaborators.Where(p => p.ID == ID).FirstOrDefault<Collaborator>();
        }

        /// <summary>
        /// Get List
        /// </summary>
        /// <returns></returns>
        public List<Collaborator> GetList()
        {
            // Get by isActive
            return dbContext.Collaborators.ToList<Collaborator>();
        }

        /// <summary>
        /// Sort Product
        /// </summary>
        /// <param name="list"></param>
        /// <param name="sortColumn"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        public List<Collaborator> Sort(List<Collaborator> list, string sortColumn, string sortOrder)
        {
            int order;

            if (sortOrder == "desc")
            {
                order = -1;
            }
            else
            {
                order = 1;
            }
            switch (sortColumn)
            {
                case "FullName":
                    list.Sort(
                         delegate(Collaborator m1, Collaborator m2)
                         { return m1.FullName.CompareTo(m2.FullName) * order; });
                    break;
                case "Gender":
                    list.Sort(
                         delegate(Collaborator m1, Collaborator m2)
                         { return m1.Gender.CompareTo(m2.Gender) * order; });
                    break;
                case "Address":
                    list.Sort(
                         delegate(Collaborator m1, Collaborator m2)
                         { return m1.Address.CompareTo(m2.Address) * order; });
                    break;
                case "Phone":
                    list.Sort(
                         delegate(Collaborator m1, Collaborator m2)
                         { return m1.Phone.CompareTo(m2.Phone) * order; });
                    break;
                case "Email":
                    list.Sort(
                         delegate(Collaborator m1, Collaborator m2)
                         { return m1.Email.CompareTo(m2.Email) * order; });
                    break;
                case "School":
                    list.Sort(
                         delegate(Collaborator m1, Collaborator m2)
                         { return m1.School.CompareTo(m2.School) * order; });
                    break;
                case "SchoolYear":
                    list.Sort(
                         delegate(Collaborator m1, Collaborator m2)
                         { return m1.SchoolYear.CompareTo(m2.SchoolYear) * order; });
                    break;
                case "CreateDate":
                    list.Sort(
                         delegate(Collaborator m1, Collaborator m2)
                         { return m1.CreateDate.CompareTo(m2.CreateDate) * order; });
                    break;
            }

            return list;
        }

        public Message Insert(Collaborator objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    // Set more info                    
                    objUI.CreateDate = DateTime.Now;                    

                    dbContext.Collaborators.InsertOnSubmit(objUI);
                    dbContext.SubmitChanges();

                    // Show success message
                    msg = new Message(MessageConstants.I0001, MessageType.Info, "Cộng tác viên '" + objUI.FullName + "'", "thêm");
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }

        
        public Message Update(Collaborator objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    // Get current group in dbContext
                    Collaborator objDb = GetById(objUI.ID);

                    if (objDb != null)
                    {
                        // Update info by objUI
                        objDb.FullName = objUI.FullName;
                        objDb.Gender = objUI.Gender;
                        objDb.Address = objUI.Address;
                        objDb.Phone = objUI.Phone;
                        objDb.Email = objUI.Email;
                        objDb.School = objUI.School;
                        objDb.SchoolYear = objUI.SchoolYear;

                        // Submit changes to dbContext
                        dbContext.SubmitChanges();
                        // Show success message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, "Cộng tác viên '" + objUI.FullName + "'", "cập nhật");

                    }
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }

            return msg;
        }

        public Message DeleteList(string ids)
        {
            Message msg = null;
            DbTransaction trans = null;            
            try
            {
                dbContext.Connection.Open();
                trans = dbContext.Connection.BeginTransaction();
                dbContext.Transaction = trans;

                if (!string.IsNullOrEmpty(ids))
                {
                    List<Collaborator> list = new List<Collaborator>();
                    ids = ids.TrimEnd(',');
                    int collaboratorID = 0;

                    string[] idArr = ids.Split(',');
                    int total = idArr.Count();
                    foreach (string id in idArr)
                    {
                        bool isValid = Int32.TryParse(id, out collaboratorID);
                        Collaborator collaborator = GetById(collaboratorID);
                        if (collaborator != null)
                        {
                            list.Add(collaborator);                            
                        }
                        else
                        {
                            total--;
                        }
                    }
                    if (list.Count > 0)
                    {
                        dbContext.Collaborators.DeleteAllOnSubmit(list);
                        dbContext.SubmitChanges();
                        // Show succes message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, total.ToString() + " cộng tác viên", "xóa");
                        trans.Commit();
                    }
                    else
                    {
                        trans.Rollback();
                    }
                }
            }
            catch (Exception)
            {
                if (trans != null) trans.Rollback();
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }

        #endregion

        #region Private methods


        #endregion
    }
}